package com.design.command;

import com.design.util.FileUtil;

import java.util.ArrayList;

/**
 * @author leali
 * @title: ConfigSettingWindow
 * @projectName design
 * @description: 配置文件设置窗口类：请求发送者
 * @date 2022/1/3 17:41
 */
public class ConfigSettingWindow {

    private final ArrayList<NetCommand> netCommands = new ArrayList<>();
    private NetCommand netCommand;

    public void setNetCommand(NetCommand netCommand) {
        this.netCommand = netCommand;
    }

    /**
     * 执行配置文件修改命令，同时将命令对象添加到命令集合中
     *
     * @param args 参数
     */
    public void call(String args) {
        netCommand.execute(args);
        netCommands.add(netCommand);
    }

    /**
     * 记录请求日志，生成日志文件，将命令集合写入日志文件
     */
    public void save() {
        FileUtil.writeCommands(netCommands);
    }

    /**
     * 从日志文件中提取命令集合，并循环调用每一个命令对象的execute()方法来实现配置文件的重新设置
     */
    public void recover() {
        ArrayList<NetCommand> commands = FileUtil.readCommands();
        assert commands != null;
        for (NetCommand obj : commands) {
            obj.execute();
        }
    }
}
